package com.cloudcc.mobile.view.web;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.cloudcc.cloudframe.bus.EventEngine;
import com.cloudcc.cloudframe.util.LogUtils;
import com.cloudcc.mobile.AppContext;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.manager.UrlManager;
import com.cloudcc.mobile.util.UrlTools;
import com.cloudcc.mobile.view.activity.BindActivity;
import com.cloudcc.mobile.weight.ui.PullToRefreshBase;
import com.cloudcc.mobile.weight.ui.PullToRefreshBase.OnRefreshListener;
import com.cloudcc.mobile.weight.ui.PullToRefreshWebView;

/**
 * 加载的webview
 * 
 * @author Administrator
 * 
 */
public class MenuWebViewActivity extends BaseWebActivity {

	private String urlString;
	private ImageView menu;
	private String obj;
	private String isFirstUrl;
	private boolean isComeFirst;
	private String mUrl;

	@Override
	protected void initUI() {
		super.initUI();
		mUrl = getIntent().getStringExtra(IWebView.KEY_URL);
		String name = getIntent().getStringExtra("obj");
		obj = name;
		mRefresh = (PullToRefreshWebView) findViewById(R.id.weixin);
		mWebView = mRefresh.getRefreshableView();
		back = (ImageView) findViewById(R.id.fanhui);
		refresh = (ImageView) findViewById(R.id.shuaxin);
		menu = (ImageView) findViewById(R.id.webview_menubtn);
		menu.setImageResource(R.drawable.fujin_back);
		ctrl_container = (RelativeLayout) findViewById(R.id.caozuo);
		ctrl_container.setVisibility(View.GONE);
		mWebView.setVisibility(View.GONE);
		addListener();
	}

	@SuppressLint("SetJavaScriptEnabled")
	@Override
	protected void initSetting() {
		super.initSetting();
		mWebView.requestFocus();
		mWebView.loadUrl(mUrl);
		mWebView.reload();
		WebSettings webstting = mWebView.getSettings();
		webstting.setJavaScriptEnabled(true);
		// 设置可以访问文件
		webstting.setAllowFileAccess(true);
		webstting.setSupportZoom(true);
		webstting.setJavaScriptCanOpenWindowsAutomatically(true);
	}

	@Override
	protected void initClient() {
		super.initClient();
		mWebView.setWebViewClient(new MyWebViewClient() {
			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				if (!isComeFirst) {
					isFirstUrl = url;
				}
				urlString = url;
				showProgress();
				if (url.equals(UrlTools.loginurl)) {
					Intent intent = new Intent(UrlTools.loginurl);
					mContext.sendBroadcast(intent);
				}
				super.onPageStarted(view, url, favicon);
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				// 加载完了再调用js登录代码
				stopProgress();
				isComeFirst = true;
				if (AppContext.isError) {
					mWebView.setVisibility(View.GONE);
					ctrl_container.setVisibility(View.GONE);
					if (!AppContext.isFirst) {
						AppContext.isError = false;
					}
					AppContext.isFirst = false;
				} else {
					AppContext.isFirst = true;
					mWebView.setVisibility(View.VISIBLE);
					ctrl_container.setVisibility(View.GONE);
				}
				super.onPageFinished(view, url);
			}

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				LogUtils.d("webview", "menu webview+跳转：" + url);
				if (UrlManager.isWapLogin(url)) {// 跳转去登录
					EventEngine.register(MenuWebViewActivity.this);
					Intent intent = new Intent(mContext, BindActivity.class);
					startActivity(intent);
					return true;
				}
				if (url.equals(UrlTools.weixinUrl + UrlTools.getFanhuiUrl(obj))
						|| url.equals(UrlTools.weixinUrl + UrlTools.FanhuiTask)
						|| url.equals(UrlTools.weixinUrl + UrlTools.FanhuiEvent)) {
					MenuWebViewActivity.this.finish();
					return true;
				} else if (url.equals(UrlTools.weixinUrl + UrlTools.wxIndexUrl)) {
					AppContext.isCloseAdd = true;
					MenuWebViewActivity.this.finish();
					return true;
				} else {
					// 自身加载新链接,不做外部跳转
					view.loadUrl(url);
					return true;
				}
			}
		});
	}

	/**
	 * //刷新
	 */
	private void addListener() {
		/**
		 * 下拉刷新webview
		 */
		mRefresh.setOnRefreshListener(new OnRefreshListener<WebView>() {
			@Override
			public void onPullDownToRefresh(
					PullToRefreshBase<WebView> refreshView) {
				mWebView.reload();
				mRefresh.onPullDownRefreshComplete();
			}

			@Override
			public void onPullUpToRefresh(PullToRefreshBase<WebView> refreshView) {
			}
		});
		back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (urlString.equals(UrlTools.weixinUrl + UrlTools.menuurl)) {
					mWebView.reload(); // 刷新
				} else {
					mWebView.goBack(); // 后退
				}
			}
		});
		refresh.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mWebView.reload(); // 刷新
			}
		});
		menu.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				MenuWebViewActivity.this.finish();
			}
		});
	}
}